package com.kmust.dream.wanderingdream.product.controller;

import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.product.entity.Product;
import com.kmust.dream.wanderingdream.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/product/product")
@Api(value = "商品API")
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    RestTemplate restTemplate;


    /**
     * 获取所有商品列表
     *
     * @return resultBean
     */
    @ApiOperation(value = "获取所有的商品", notes = "无参数")
    @GetMapping("/getAllProducts")
    public ResultBean getAllProducts() {
        return productService.getAllProducts();
    }

    /**
     * 分页获取商品
     *
     * @param current 当前页
     * @param size    每页大小
     * @return resultBean
     */
    @ApiOperation(value = "分页获取商品", notes = "参数为需求页，每页大小（需要进行分页计算),type为0是全部分页，其余为商品种类id")
    @GetMapping("/getProductsPages/{type}/{current}/{size}")
    public ResultBean getProductsPages(@PathVariable Integer type, @PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        return productService.getProductsPages(type, current, size);
    }


    @ApiOperation("根据主键获取product对象")
    @GetMapping("/getProductById/{productId}")
    public ResultBean getProductById(@PathVariable Integer productId) {
        return productService.getProductById(productId);
    }

    /**
     * 添加商品
     *
     * @param product 商品
     * @return resultBean
     */
    @ApiOperation(value = "添加商品", notes = "参数是商品的json字符串")
    @PostMapping("/addProduct")
    public ResultBean addProduct(@RequestBody Product product) {
        return productService.addProduct(product);

    }

    /**
     * 修改商品
     *
     * @param product 商品
     * @return resultBean
     */
    @ApiOperation(value = "更新商品", notes = "参数是商品的json字符串，底层使用byid查询，id不要变动")
    @PutMapping("/updateProduct")
    public ResultBean updateProduct(@RequestBody Product product) {
        return productService.updateProduct(product);
    }

    /**
     * 假删除
     *
     * @param productId 商品id
     * @return resultBean
     */
    @ApiOperation(value = "对商品进行假删除", notes = "参数是商品的主键productId")
    @DeleteMapping("/deleteProductById/{id}")
    public ResultBean deleteProductById(@PathVariable("id") Integer productId) {
        return productService.deleteProductById(productId);
    }

    @ApiOperation(value = "通过商品分类主键查询商品")
    @GetMapping("/getAllProductByCategoryId/{categoryId}")
    public ResultBean getAllProductByCategoryId(@PathVariable Integer categoryId) {
        return productService.getAllProductByCategoryId(categoryId);
    }

    @ApiOperation(value = "获取商品最大的id", notes = "无参数")
    @GetMapping("/getMaxId")
    public ResultBean getMaxId() {
        return productService.getMaxId();
    }
}
